// pages/tabBar/my/set/set.js
const util = require("../../../../utils/util");
const http = require("../../../../utils/httpUtils");
const qiniuUploader = require("../../../../utils/qiniuUploader");
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '设置' //导航栏 中间的标题
        },
        change: true,
        anchorTargetText: '修改',
        height: app.globalData.height * 2 + 20,
        allImages: [],
        date: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            user: app.globalData.userInfo
        })
    },
    // 退出登录
    exitLogin() {
        wx.showModal({
            title: '提示',
            content: '您确定要退出吗？',
            success: res => {
                if (res.confirm) {
                    wx.setStorage({
                        key: 'token',
                        data: '',
                        success: res => {
                            wx.clearStorage() //清楚所有数据缓存
                            app.globalData.token = ''
                            wx.reLaunch({
                                url: '/pages/public/login/login',
                            })
                        }
                    })
                }
            }
        })
    },
    changeHeadimg() {
        this.getQiniuToken() //获取token并上传
    },
    getQiniuToken() { //获取七牛云token
        http._headerGet('/api/upload/getQiniuToken', {}, res => {
            if (res.data.code == 1) {
                this.data.QiNiuToken = res.data.data.QiNiuToken
            }
        })
        wx.chooseImage({ //选择图片上传
            count: 1,
            success: res => {
                wx.showLoading({
                    title: '上传头像中',
                })
                // 循环上传
                for (let i = 0; i < res.tempFilePaths.length; i++) {
                    let filePath = res.tempFilePaths[i];
                    this.data.date = util.formatTime(new Date()).substring(0, 10) // 设置时间字符串
                    let uploadKey = (util.formatTime(new Date()) + Math.floor(Math.random() * 99999) + i).replace(/\-/g, '')
                    // 上传七牛云
                    qiniuUploader.upload(filePath, (res) => {
                            let imgRes = res;
                            http._headerPost('/api/user/setHeadPortrait', {
                                'headImg': imgRes.file_path,
                                'status': imgRes.code
                            }, res => {
                                if (res.data.code == 1) {
                                    wx.showModal({
                                        title: '提示',
                                        content: res.data.msg,
                                        showCancel: false,
                                        success: res => {
                                            let pages = getCurrentPages(); // 当前页面
                                            let beforePage = pages[pages.length - 2]; // 前一个页面
                                            wx.navigateBack({
                                                delta: 2,
                                                success() {
                                                    beforePage.onLoad(); // 执行前一个页面的onLoad方法
                                                }
                                            })
                                        }
                                    })
                                } else {
                                    wx.showToast({
                                        title: res.data.msg,
                                        icon: none,
                                        duration: 2000
                                    })
                                }
                                wx.hideLoading()
                            })
                        }, (error) => {
                            wx.hideLoading()
                            console.log('error: ' + error);
                        }, {
                            region: 'NCN', //區域代碼 https://up-z1.qbox.me
                            key: `${this.data.date}/${uploadKey}.png`, //文件命名
                            uptoken: this.data.QiNiuToken,
                            domain: 'http://qn.cgxczx.com',
                            shouldUseQiniuFileName: false
                        },
                        (res) => {
                            console.log(res.progress)
                        }, cancelTask => this.setData({
                            cancelTask
                        })
                    );
                }
            }
        })
    },
    changeText(e) {
        console.log(e)
        if (this.data.anchorTargetText == '修改') {
            this.setData({
                change: false,
                anchorTargetText: '保存'
            })

        } else {
            wx.showLoading({
                title: '修改中'
            })
            if (this.data.nickname) {
                http._headerPost('/api/user/setUserNickname', {
                    'nickname': this.data.nickname
                }, res => {
                    // console.log(res);
                    if (res.data.code == 1) {
                        this.pageReturn(res)
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: res.data.msg,
                            showCancel: false
                        })
                    }
                })
                this.setData({
                    change: true,
                    anchorTargetText: '修改'
                })
            } else {
                wx.hideLoading()
                wx.showToast({
                    title: '昵称不能为空哦',
                    image: '/image/icon/info.png',
                    duration: 2000
                })
            }
        }
    },
    recommender(e) { //绑定推荐人
        if (e.currentTarget.dataset.user == '0') {
            wx.navigateTo({
                url: '/pages/public/recommender/index',
            })
        } else {
            wx.showToast({
                title: '已经绑定过推荐人',
                image: '/image/icon/info.png',
                duration: 2000
            })
        }

    },
    forget_password() { //修改密码
        wx.navigateTo({
            url: '/pages/public/forget_password/forget_password',
        })
    },
    address() { //地址管理
        wx.navigateTo({
            url: '/pages/public/address/address?type=look&mold=user',
        })
    },
    nickname(e) {
        this.setData({
            nickname: e.detail.value
        })
    },
    pageReturn(res) { // 返回
        wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            success: res => {
                let pages = getCurrentPages(); // 当前页面
                let beforePage = pages[pages.length - 2]; // 前一个页面
                wx.navigateBack({
                    delta: 2,
                    success() {
                        beforePage.onLoad(); // 执行前一个页面的onLoad方法
                    }
                })
            }
        })
    }
})